﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for PropertyGridManager.xaml
    /// </summary>
    public partial class PropertyGridManager : UserControl
    {
        SortedDictionary<ShaderBase, PropertyGrid> shaderGridDict = null;
        PropertyGrid currGrid = null;

        wpfTool.Window1 MainWindow = null;

        PropertyGrid CurrentGrid
        {
            get { return currGrid; }
            set
            {
                if(currGrid != null)
                    currGrid.Visibility = Visibility.Collapsed;

                currGrid = value;
                currGrid.Visibility = Visibility.Visible;
            }
        }

        // should never be used!
        public PropertyGridManager()
        {
            InitializeComponent();

            throw new System.Exception("Wrong Ctor!");
        }

        public PropertyGridManager(wpfTool.Window1 wnd)
        {
            InitializeComponent();

            MainWindow = wnd;

            shaderGridDict = new SortedDictionary<ShaderBase, PropertyGrid>();
        }

        public void createShaderPropertyGrid(ShaderBase shader)
        {
            shaderGridDict[shader] = new PropertyGrid(shader);
            shaderGridDict[shader].ShaderPropertyChange += new Window1.ShaderPropertyChangeHandler(PropertyGridManager_ShaderPropertyChange);
            CurrentGrid = shaderGridDict[shader];

            CurrentGrid.VerticalAlignment = VerticalAlignment.Top;
            CurrentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
            Binding binding = new Binding();
            binding.Source = MainWindow.propertyGridWindow;
            binding.Path = new PropertyPath("Width");
            CurrentGrid.SetBinding(WidthProperty, binding);

            MainWindow.propertyGridWindow.Children.Add(currGrid);
        }

        void PropertyGridManager_ShaderPropertyChange(ShaderBase shader)
        {
            MainWindow.shaderMgr.updatePipeline(MainWindow.pipelineViewMgr.CurrentPipeline, shader);
        }

        public void OnShaderSelected(ShaderBase shader)
        {
            if (!shaderGridDict.ContainsKey(shader))
                return;

            CurrentGrid = shaderGridDict[shader];
        }
    }
}
